home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / class / file.c < prev    next >
C/C++ Source or Header  |  1997-04-16  |  5KB  |  226 lines

  1.  
  2.  
  3. /*  Copyright (c) 1993-1996 Algorithms Corporation  */
  4. /*  All rights reserved.  */
  5.  
  6.  
  7.  
  8.  
  9. /*  This file automatically generated by dpp - do not edit  */
  10.  
  11. #define    DPP_STRATEGY    2
  12. #define    DPP_FASTWIDE    0
  13.  
  14.  
  15.  
  16. #line 16 "file.d"
  17. #include <sys/types.h> 
  18. #if !defined(vms) && !defined(__MWERKS__) 
  19. #include <sys/stat.h> 
  20. #endif 
  21. #ifdef sparc 
  22. #include <unistd.h> 
  23. #endif 
  24.  
  25. #define    CLASS    File_c
  26. #define    ivType    File_iv_t
  27.  
  28. #include "generics.h"
  29.  
  30. object    File_c;
  31.  
  32.  
  33. #line 34 "file.c"
  34. typedef struct  _File_iv_t  {
  35.     object iName;
  36.     FILE * iFP;
  37. }    File_iv_t;
  38.  
  39.  
  40.  
  41. #line 31 "file.d"
  42. cmeth objrtn File_cm_gNew(object self)
  43.     return gShouldNotImplement(self, "gNew"); 
  44.  
  45. cmeth objrtn File_cm_gOpenFile(object self, char *name, char *mode)
  46.     object obj; 
  47.     ivType *iv; 
  48.     FILE *fp; 
  49.  
  50.     if (IsObj((object) name)) 
  51.         name = gStringValue((object) name); 
  52.     if (IsObj((object) mode)) 
  53.         mode = gStringValue((object) mode); 
  54.     fp = fopen(name, mode); 
  55.     if (!fp) 
  56.         return NULL; 
  57.     obj = oSuper(File_c, gNew, self)(self); 
  58.     iv = ivPtr(obj); 
  59.     iv->iName = gNewWithStr(String, name); 
  60.     iv->iFP = fp; 
  61.     return obj; 
  62.  
  63. static objrtn StreamObject(object self, char *name, FILE *fp)
  64.     object obj; 
  65.     ivType *iv; 
  66.  
  67.     obj = oSuper(File_c, gNew, self)(self); 
  68.     iv = ivPtr(obj); 
  69.     iv->iName = gNewWithStr(String, name); 
  70.     iv->iFP = fp; 
  71.     return obj; 
  72.  
  73. imeth objrtn File_im_gDispose(object self)
  74. { File_iv_t *iv = GetIVs(File, self);
  75.     fclose(iv->iFP); 
  76.     gDispose(iv->iName); 
  77.     oSuper(File_c, gDispose, self)(self); 
  78.     return NULL; 
  79.  
  80. imeth int File_im_gRead(object self, char *buf, unsigned n)
  81. { File_iv_t *iv = GetIVs(File, self);
  82.     return fread(buf, 1, n, iv->iFP); 
  83.  
  84. imeth int File_im_gWrite(object self, char *buf, unsigned n)
  85. { File_iv_t *iv = GetIVs(File, self);
  86.     return fwrite(buf, 1, n, iv->iFP); 
  87.  
  88. imeth char * File_im_gGets(object self, char *buf, int sz)
  89. { File_iv_t *iv = GetIVs(File, self);
  90.  
  91.     return fgets(buf, sz, iv->iFP); 
  92.  
  93. imeth long File_im_gAdvance(object self, long n)
  94. { File_iv_t *iv = GetIVs(File, self);
  95.     int r = fseek(iv->iFP, n, SEEK_CUR); 
  96.     return r ? 0L : n; 
  97.  
  98. imeth long File_im_gRetreat(object self, long n)
  99. { File_iv_t *iv = GetIVs(File, self);
  100.     int r = fseek(iv->iFP, -n, SEEK_CUR); 
  101.     return r ? 0L : n; 
  102.  
  103. imeth long File_im_gSeek(object self, long n)
  104. { File_iv_t *iv = GetIVs(File, self);
  105.     int r = fseek(iv->iFP, n, SEEK_SET); 
  106.     return r ? 0L : n; 
  107.  
  108. imeth long File_im_gPosition(object self)
  109. { File_iv_t *iv = GetIVs(File, self);
  110.  
  111.     return ftell(iv->iFP); 
  112.  
  113. imeth long File_im_gLength(object self)
  114. { File_iv_t *iv = GetIVs(File, self);
  115. #if !defined(vms) && !defined(__MWERKS__) 
  116.         struct stat sb; 
  117.     int r; 
  118.  
  119.     r = fstat(fileno(iv->iFP), &sb); 
  120.     return r ? -1L : sb.st_size; 
  121. #else 
  122.         long sav = ftell(iv->iFP); 
  123.     long len; 
  124.  
  125.     fseek(iv->iFP, 0L, SEEK_END); 
  126.     len = ftell(iv->iFP); 
  127.     fseek(iv->iFP, sav, SEEK_SET); 
  128.     return len; 
  129. #endif 
  130.     } 
  131.  
  132. imeth char * File_im_gName(object self)
  133. { File_iv_t *iv = GetIVs(File, self);
  134.     return gStringValue(iv->iName); 
  135.  
  136. imeth int File_im_gEndOfStream(object self)
  137. { File_iv_t *iv = GetIVs(File, self);
  138.     return feof(iv->iFP); 
  139.  
  140. imeth void * File_im_gPointerValue(object self)
  141. { File_iv_t *iv = GetIVs(File, self);
  142.     return (void *) iv->iFP; 
  143.  
  144. static void class_init(void) 
  145.     String; 
  146.     stdoutStream_o = StreamObject(CLASS, "stdout", stdout); 
  147.     stderrStream_o = StreamObject(CLASS, "stderr", stderr); 
  148.     stdinStream_o = StreamObject(CLASS, "stdin", stdin); 
  149.     traceStream_o = stdoutStream_o; 
  150.  
  151. #line 171 "file.c"
  152.  
  153. objrtn    File_initialize(void)
  154. {
  155.     static  CRITICALSECTION  cs;
  156.     static  int volatile once = 0;
  157.  
  158.     ENTERCRITICALSECTION(_CI_CS_);
  159.     if (!once) {
  160.         INITIALIZECRITICALSECTION(cs);
  161.         once = 1;
  162.     }
  163.     LEAVECRITICALSECTION(_CI_CS_);
  164.  
  165.     ENTERCRITICALSECTION(cs);
  166.  
  167.     if (File_c) {
  168.         LEAVECRITICALSECTION(cs);
  169.         return File_c;
  170.     }
  171.     INHIBIT_THREADER;
  172.     Stream_initialize();
  173.     if (File_c)  {
  174.         ENABLE_THREADER;
  175.         LEAVECRITICALSECTION(cs);
  176.         return File_c;
  177.     }
  178.     File_c = gNewClass(Class, "File", sizeof(File_iv_t), 0, Stream, END);
  179.     cMethodFor(File, gNew, File_cm_gNew);
  180.     cMethodFor(File, gOpenFile, File_cm_gOpenFile);
  181.     iMethodFor(File, gRetreat, File_im_gRetreat);
  182.     iMethodFor(File, gPosition, File_im_gPosition);
  183.     iMethodFor(File, gLength, File_im_gLength);
  184.     iMethodFor(File, gWrite, File_im_gWrite);
  185.     iMethodFor(File, gEndOfStream, File_im_gEndOfStream);
  186.     iMethodFor(File, gSeek, File_im_gSeek);
  187.     iMethodFor(File, gPointerValue, File_im_gPointerValue);
  188.     iMethodFor(File, gAdvance, File_im_gAdvance);
  189.     iMethodFor(File, gDispose, File_im_gDispose);
  190.     iMethodFor(File, gName, File_im_gName);
  191.     iMethodFor(File, gGets, File_im_gGets);
  192.     iMethodFor(File, gGCDispose, File_im_gDispose);
  193.     iMethodFor(File, gDeepDispose, File_im_gDispose);
  194.     iMethodFor(File, gRead, File_im_gRead);
  195.  
  196.     class_init();
  197.  
  198.     ENABLE_THREADER;
  199.  
  200.     LEAVECRITICALSECTION(cs);
  201.  
  202.     return File_c;
  203. }
  204.  
  205.  
  206.  
  207.